home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: Multiplication Pr 1/2
- Date: 1 Feb 1996 10:08:55 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4eq3fn$oip@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- agent439@aol.com (Agent439)
- in <4ep07m$4fn@newsbf02.news.aol.com> asks:
-
- >I'm trying to write a C Program that will accept a positive integer that
- >can be 100 digits long at a maximum. So far, my first example (INPUT
-
- Sigh. Since you are storing your numbers in unsigned longs (which have
- a length of k bits), the largest _result_ must fit in k bits, about
- 0.3k digits. If k = 64 then .3k = 19.2, so you can see that 100 digits is
- excessive.
-
- >3125, 25) works except that when it tries to print out the answer, the
-
- "works" for a value of "works" that approaches zero. There are a number
- of errors which should keep your code from doing anything useful.
-
- >number is wrong. Now I know there are 2 warnings in it, but I watched
-
- One would _hope_ to see a lot more than 2 warnings for this code. Turn
- _all_ your warnings back on. BTW, there are several aspects of this
- code which show that you have not read the FAQ, as you should have before
- posting. It is available by anonymous ftp from rtfm.mit.edu. [These aspects
- include void main(), using gets() for input, and the syntax error " //"].
- Quickly rectify this before you get labeled as a clueless aol-er.
-
- The comparisons of char *s with 0 to determine whether they represent
- negative numbers is interestingly creative, if completely meaningless.
-
- >variables step-by-step, and it seems to get the right answer. Please
-
- How?
-
- >disregard the timer part, and any disagreements what I call my variables.
- >Just tell me what types should my variables be or what printf conversion
- >should be in for the answer? Any suggestions to get rid of the warnings
- >would be appreciated too. :-0
-
- BTW, an 8-line .sig is excessive. Try to restrain your enthusiasms to
- for various games, sports teams, and radio stations to a 4-line .sig.
-
- Your original code is at EOM.
- Omitting your timing stuff, the following does what you are _trying_
- to do in your code.
-
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- char a[100], b[100];
- unsigned long pa, pb, c;
-
- printf("Enter the Multiplicand:");
- fflush(stdout);
- fgets(a, sizeof a, stdin);
- printf("Enter the Multiplier:");
- fflush(stdout);
- fgets(b, sizeof b, stdin);
-
- pa = atol(a);
- pb = atol(b);
- c = pa * pb;
- printf("The product is =======> %lu \n", c);
- return 0;
- }
-
- [ === the original code from agent439@aol.com === ]
- >Here is the program
- >#include <math.h>
- >#include <stdio.h>
- >#include <time.h>
-
- >#define FALSE 0
- >#define TRUE !FALSE
-
- >float timer(int reset);
- >void main(){
-
- > char a[100],b[100];
- > int i, j;
- > int x, y, z;
- > int size,truesize;
- > unsigned long pa,pb, c;
-
- > a==NULL;
- > b==NULL;
- > printf("Enter the Multiplicand:");
- > gets(a);
- > while(a<0){
- > printf("Positive Integers only please!");
- > printf("Enter the Multiplicand:");
- > gets(a);
- > }
-
- > printf("Enter the Multiplier:");
- > gets(b);
- > while(b<0){
- > printf("Positive Integers only please!");
- > printf("Enter the Multiplier:");
- > gets(b);
- > }
- > size=0;
- > pa=0;
- > while(a[size] != 0){
- > size++;
- > }
- > size--;
- > truesize=size;
- > for(size==size;size>=0;size--){
- > pa += (a[size]-48)*(pow(10,(truesize-size))); //change char to integer
- >value
- > }
- > size=0;
- > pb=0;
- > while(b[size] != 0){
- > size++;
- > }
- > size--;
- > truesize=size;
- > for(size==size;size>=0;size--){
- > pb += (b[size]-48)*(pow(10,(truesize-size))); //change char to
- >integer value
- > }
- > //timer(TRUE);
- > c=0;
- > c+=(pa);//Start C with the amount in pa
- > c*=(pb); //Multiply pa by the integer value of pb
- > printf("The product is =======> %i \n",c);
- > for(i=0;i<1000;i++){
- > for(j=0;j<1000;j++){
- > z= x++ + ++y;
- > }
- > }
- > printf("Elapsed time = %.2f seconds\n", timer(FALSE));
-
- > timer(TRUE);
- > for(i = 0; i < 1000; i++){
- > for (j = 0; j < 1000; j++){
- > y++;
- > z = x + y;
- > x++;
- > }
- > }
- > printf("Elapsed time = %.2f seconds\n", timer(FALSE));
- >}
- >>> Continued to next message
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-